package com.ntdpmap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {
	
	//Database Version
	private static final int DATABASE_VERSION = 1;
	
	//Database Name
	private static final String DATABASE_NAME = "RoomDB";
	
	
	
	public MySQLiteHelper (Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		//SQL statement to create room table
		String CREATE_BOOK_TABLE = "CREATE TABLE rooms ( " +
				"id INTEGER PRIMARY KEY AUTOINCREMENT, "+
				"ROOM_NUMBER TEXT, " +
				"ROOM_LAT TEXT, " +
				"ROOM_LONG TEXT )";
		
		
		//create table
		db.execSQL(CREATE_BOOK_TABLE);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS rooms");
		this.onCreate(db);
	}
	
	
	
	// Room table name
    private static final String TABLE_ROOMS = "rooms";
 
    // Rooms Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_ROOM = "ROOM_NUMBER";
    private static final String KEY_LAT = "ROOM_LAT";
    private static final String KEY_LONG = "ROOM_LONG";
    
    private static final String[] COLUMNS = {KEY_ID,KEY_ROOM,KEY_LAT,KEY_LONG};

    
	public void addRoom( String Room, double Lat, double Long){
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(KEY_ROOM, Room);
		values.put(KEY_LAT, String.valueOf(Lat));
		values.put(KEY_LONG, String.valueOf(Long));
		
		db.insert("rooms", null, values);
		
		db.close();	
	}
	
	public double getLat(String room){
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = 
				db.query(TABLE_ROOMS,
						COLUMNS,
						" ROOM_NUMBER = " + room,
						null,
						null,
						null,
						null,
						null);
		
		if (cursor != null)
				cursor.moveToFirst();
		
		double Lat = (Double.parseDouble(cursor.getString(3)));
		return Lat;
	}
	
	public double getLong(String room){
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor =
				db.query (TABLE_ROOMS,
				COLUMNS,
				" ROOM_NUMBER = " + room,
				null,
				null,
				null,
				null,
				null);
		if (cursor != null)
				cursor.moveToFirst();
		
		double Long = (Double.parseDouble(cursor.getString(4)));
		return Long;
	}
}